package com.yewen.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;

import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Map;

/**
 * @Classname HMACEncryptUtil
 * @Description token加密工具
 * @Date 2022/3/21 23:18
 * @Created by TaoVh
 */
public final class HMACEncryptUtil {
    private static Algorithm algorithmHS;
    static {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA256");
            SecureRandom random  = new SecureRandom();
            keyGenerator.init(random);
            Key key = keyGenerator.generateKey();
            algorithmHS = Algorithm.HMAC256(key.getEncoded());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    public static String generateToken(String userID,boolean isLogin){
        return JWT.create().
                withIssuer(userID).
                withClaim("isLogin",isLogin).
                sign(algorithmHS);
    }
    public static String generateToken(String userID,boolean isLogin,boolean isEduCounselor){
        return JWT.create().
                withIssuer(userID).
                withClaim("isLogin",isLogin).
                withClaim("isEduCounselor",isEduCounselor).
                sign(algorithmHS);
    }

    @Deprecated
    public static boolean verify(String token){
        try {
            JWTVerifier verifier = JWT.require(algorithmHS)
                    .withClaim("isLogin",true)
                    .build();
            verifier.verify(token);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public static boolean verify(String token,String property,boolean value){
        try {
            JWTVerifier verifier = JWT.require(algorithmHS)
                    .withClaim(property,value)
                    .build();
            verifier.verify(token);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public static boolean verify(String token,String property,Integer value){
        try {
            JWTVerifier verifier = JWT.require(algorithmHS)
                    .withClaim(property,value)
                    .build();
            verifier.verify(token);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public static boolean verify(String token,String property,Double value){
        try {
            JWTVerifier verifier = JWT.require(algorithmHS)
                    .withClaim(property,value)
                    .build();
            verifier.verify(token);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public static boolean verify(String token,String property,String value){
        try {
            JWTVerifier verifier = JWT.require(algorithmHS)
                    .withClaim(property,value)
                    .build();
            verifier.verify(token);
            return true;
        }catch (Exception e){
            return false;
        }
    }

}
